<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\GroupListParams;
use App\Params\Backend\GroupUpdateParam;
use App\Services\Base\GroupService;
use App\Services\Goods\CategoryService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;
/**
 * 系统分组控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class GroupController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {

    }

    /**
     * 保存分组
     *
     * @param Request $request
     * @throws
     * @return
     */
    public function update()
    {
        $param = GroupUpdateParam::create();
        $ret = GroupService::getInstance()->update($param);
        if($ret) {
            return $this->success(['group_id' => $ret]);
        }
        return $this->fail(ResponseCode::SAVE_FAIL);
    }

    /**
     * 分组列表
     *
     * @throws
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        $param = GroupListParams::create();
        $data = GroupService::getInstance()->getList($param);
        return $this->success($data);
    }

    /**
     * 删除分组
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function delete()
    {
        $categoryId = $this->verifyInteger('group_id');
        $ret = GroupService::getInstance()->delete($categoryId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::CATEGORY_DELETE_FAIL);
    }

    /**
     * 获取分组
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function get()
    {
        $id = $this->verifyInteger('id');
        $ret = GroupService::getInstance()->get($id);
        return $this->success($ret);
    }

    /**
     * 停用/启用
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function enable()
    {
        $id = $this->verifyInteger('id');
        $ret = CategoryService::getInstance()->enable($id);
        return $this->success($ret);
    }
}
